package com.bestcem.xm.wallet.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.deliver.grpc.v1.services.BpDeliverTicketServiceGrpc;
import com.bestcem.xm.deliver.grpc.v1.services.GetSmsBalanceByOrgIdRequest;
import com.bestcem.xm.deliver.grpc.v1.services.GetSmsBalanceByOrgIdResponse;
import com.bestcem.xm.wallet.grpc.client.converter.WalletSmsConverter;
import com.bestcem.xm.wallet.grpc.client.dto.SmsBalanceDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * 获取短信余额服务
 *
 * @author panyun <yun.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/20 16:17
 */
@Slf4j
@Service(value = "walletSmsBalanceGrpcService")
public class WalletSmsBalanceGrpcService {

    @GrpcClient(GrpcClientNames.DS_DELIVER)
    private BpDeliverTicketServiceGrpc.BpDeliverTicketServiceBlockingStub stub;

    /**
     * 根据org_id 获取 短信余额
     *
     * @param orgId 短信任务标识
     * @return com.bestcem.xm.user.grpc.client.dto.SmsTaskInfoDTO
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/20 16:36
     */
    public SmsBalanceDTO getSmsBalanceByOrgId(String orgId) {
        if (log.isInfoEnabled()) {
            log.info("获取短信余额：id:{} ", orgId);
        }
        GetSmsBalanceByOrgIdRequest request = GetSmsBalanceByOrgIdRequest.newBuilder().setOrgId(orgId).build();
        GetSmsBalanceByOrgIdResponse response = stub.getSmsBalanceByOrgId(request);
        // 校验查询结果
        if (Code.ERROR_VALUE == response.getStatus().getCodeValue()) {
            log.error("获取短信余额失败：id:{}", orgId);
            return null;
        }
        return WalletSmsConverter.responseToSmsTaskInfoDto(response);
    }

}
